<template>
  <a-modal
    :title="modelTitle"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="权限名"
          help="该名称用于定义权限标识，用于权限判断"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-input
            v-decorator="['name', {rules: [{required: true, message: '权限名不能为空'}]}]"
            placeholder="例如：admin.index.index" />
        </a-form-item>
        <a-form-item
          label="权限显示名"
          help="该名称用于查看"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-input
            v-decorator="['display_name', {rules: [{required: true, message: '权限显示名不能为空'}]}]"
            placeholder="请输入权限显示名" />
        </a-form-item>
        <a-form-item
          label="描述"
          help="权限说明"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-textarea
            v-decorator="['description']"
            :autoSize="{ minRows: 2, maxRows: 6 }"
            placeholder="输入权限描述" />
        </a-form-item>
        <a-form-item
          label="排序"
          help="数字越小越靠前"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-input-number
            v-decorator="['order_num', { initialValue: 0 }]"
            :min="0" />
        </a-form-item>
        <a-form-item
          label="是否记录操作日志"
          help="该选项用于控制是否记录管理员操作日志"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-switch v-decorator="['is_opertion_log', { valuePropName: 'checked' }]" />
        </a-form-item>
        <a-form-item
          label="是否为公共权限"
          help="选择是表示该权限无需验证，全局可用"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-switch v-decorator="['is_public', { valuePropName: 'checked' }]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { saveData } from '@/api/system/syspermission'
import { pick } from 'lodash'
export default {
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      },
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      id: 0,
      parentId: 0
    }
  },
  computed: {
    modelTitle: function () {
      if (this.id === 0) {
        if (this.parentId === 0) {
          return '新建权限'
        } else {
          return '新建子权限'
        }
      } else {
        return '编辑权限'
      }
    }
  },
  methods: {
    add () {
      this.visible = true
      this.id = 0
      this.parentId = 0
      const { form: { resetFields } } = this
      this.$nextTick(() => {
        resetFields()
      })
    },
    addChild (record) {
      if (!record) {
        this.$message.error('请选中您想要添加子节点的权限')
        return false
      }
      this.visible = true
      this.id = 0
      this.parentId = record.perm_id
      const { form: { resetFields } } = this
      this.$nextTick(() => {
        resetFields()
      })
    },
    edit (record) {
      if (!record) {
        this.$message.error('请选中您想要编辑的权限')
        return false
      }
      this.visible = true
      this.id = record.perm_id
      this.parentId = record.parent_id
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        setFieldsValue(pick(record, ['name', 'display_name', 'description', 'order_num', 'is_public', 'is_opertion_log']))
      })
    },
    handleSubmit () {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          saveData(Object.assign(values, { perm_id: this.id, parent_id: this.parentId })).then(res => {
            this.confirmLoading = false
            if (res.code === 0) {
              this.visible = false
              this.$message.success(res.message)
              this.$emit('ok', values)
            } else {
              this.$message.error(res.message)
            }
          })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.visible = false
    }
  }
}
</script>
